package etl.test.xml;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

public class XMLCof {

	private static final Logger logger = Logger.getLogger(XMLCof.class);

	private static String CONFIG_FILE_NAME = "extract-mapping.xml";

	private static Map<String, Map<String, MappingBean>> extractMapping;

	public static void main(String[] args) {

		extractMapping = new HashMap<String, Map<String, MappingBean>>();
		XMLConfiguration config;

		try {
			config = new XMLConfiguration(CONFIG_FILE_NAME);

			config.setDelimiterParsingDisabled(true);
			List ls = config.getList("start-mapping.mapping.jobtype");
			for (int i = 0; i < ls.size(); i++) {
				MappingBean mapping = new MappingBean();

				String jobtype = config.getString("start-mapping.mapping(" + i
						+ ").jobtype");
				String filetype = config.getString("start-mapping.mapping(" + i
						+ ").filetype");
				String extractclass = config.getString("start-mapping.mapping("
						+ i + ").extractclass");
				String loadclass = config.getString("start-mapping.mapping("
						+ i + ").loadclass");
				mapping.setExtractclass(extractclass);
				mapping.setFileType(filetype);
				mapping.setJobType(jobtype);
				mapping.setLoadclass(loadclass);
				Map<String, MappingBean> map = extractMapping.get(jobtype);
				if (map == null) {
					map = new HashMap<String, MappingBean>();
					extractMapping.put(jobtype, map);
				}
				map.put(filetype, mapping);
				
			}
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		
		print();
	}
	
	public static void print() {
		//使用entrySet的方式遍历效率较传统的遍历方式要高
		Iterator iterator = extractMapping.entrySet().iterator();
		
		while (iterator.hasNext()) {
			
			Entry<String, Map<String,MappingBean>> entry = (Entry<String, Map<String,MappingBean>>) iterator.next();
			//Object key = entry.getKey();
			Map<String,MappingBean> value = entry.getValue();
			
			Iterator it = value.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, MappingBean> entry2 = (Entry<String, MappingBean>) it.next();
				MappingBean mBean = entry2.getValue();
				System.out.println(mBean.getJobType());
				System.out.println(mBean.getFileType());
				System.out.println(mBean.getExtractclass());
				System.out.println(mBean.getLoadclass());
				System.out.println("++++++++++++++++++++++");
			}
		}
	}
	private static class MappingBean {

		private String jobType;

		private String fileType;

		private String extractclass;

		private String loadclass;

		public String getJobType() {
			return jobType;
		}

		public void setJobType(String jobType) {
			this.jobType = jobType;
		}

		public String getFileType() {
			return fileType;
		}

		public void setFileType(String fileType) {
			this.fileType = fileType;
		}

		public String getExtractclass() {
			return extractclass;
		}

		public void setExtractclass(String extractclass) {
			this.extractclass = extractclass;
		}

		public String getLoadclass() {
			return loadclass;
		}

		public void setLoadclass(String loadclass) {
			this.loadclass = loadclass;
		}

	}
}
